const express=require('express');
const mysql=require('mysql');
const urlobj=require('url');
//引用自定义
//const commte=require('../../libs/commtle');
//链接数据库
var db=mysql.createPool({
    host:'localhost',
    user:'root',
    password:'',
    database:'leasedb'
});

//==============1最新动态==========
//业务逻辑
module.exports = function() {
    var router = express.Router();
    //查询最新动态
    router.get('/forumcomment.html',function(req,res,next) {
        var delelt = +req.query.forumAId;

        //格式化url栏的参数，转换为对像
        //用户确定要删除，查询当前对应的是否有商品,没有商品就删除成功
        if (urlobj.parse(req.url, true).query.act == null || urlobj.parse(req.url, true).query.act == "") {
            db.query('SELECT * FROM forumcomment,forumbinfo,userinfo where forumbinfo.forumBId=forumcomment.forumBId and userinfo.userId=forumcomment.userId', function (err, data) {
                if (err) {
                    res.status(400).send('<script>alert("数据查询失败，请检查后再次查询!");window.location.href="404.html";</script>').end();
                }
                else {
                    if (data.length == 0) {
                        res.status(400).send('<script>alert("暂无可用数据!");window.location.href="404.html";</script>').end();
                    }
                    else {
                        req.msg = data;

                    }
                    next();
                }
            });
        }
        else if (urlobj.parse(req.url, true).query.act == "delete") {
            var delelt = +req.query.forumAId;
            db.query("DELETE FROM forumcomment WHERE FCId=" + parseInt(req.query.FCId), function (err, data) {
                if (err) {
                    res.status(500).send('<script>alert("该分类下游数据!");window.location.href="forumcomment.html";</script>').end();
                }
                else {
                    res.status(200).send('<script>alert("删除成功!");window.location.href="forumcomment.html";</script>').end();
                }

            });
        }
    });

   //开始渲染后台首页
    router.use('/forumcomment.html',function(req,res,next){
        res.render("forum/forumcomment.ejs",{"msg":req.msg});
    });
    return router;
}